<?php
/**
 * Created by PhpStorm.
 * User: plume
 * Date: 2017/11/20
 * Time: 16:33
 */
namespace App\Libraries;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class HTTPHandler
{
    function __construct()
    {

    }

    static public function http_get_data($url)
    {
        //初始化
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        //执行并获取HTML文档内容
        $output = curl_exec($ch);

        $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        //释放curl句柄
        curl_close($ch);
        //打印获得的数据
        if($return_code=="200") {
            return $output;
        }
        return false;
    }

    static public function http_post_json($url, $data_string)
    {

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($data_string))
        );
        ob_start();
        curl_exec($ch);
        $return_content = ob_get_contents();
        ob_end_clean();
        $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return array($return_code, $return_content);
    }

    public static function http_post($url, $data, $header = 0)
    {
        $opt = array(
            CURLOPT_URL => $url,
            CURLOPT_HEADER => $header,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_TIMEOUT => 60,
            CURLOPT_POSTFIELDS => http_build_query($data),
            CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.29 Safari/525.13'
        );
        return HTTPHandler::curl_run($opt);
    }

    public static function curl_run($opt)
    {
        $ch = curl_init();
        curl_setopt_array($ch, $opt);
        $r = curl_exec($ch);
        curl_close($ch);
        return $r;
    }

    static public function getAccessTokenQYH() {
        $token = Cache::get("qyh_access_token", null);
        $corpid = \Illuminate\Support\Facades\DB::table('settings')->where("name", "QYH_CORPID")->value('value');
        $secret = \Illuminate\Support\Facades\DB::table('settings')->where("name", "QYH_SECRET")->value('value');
        if(empty($token)) {
            $get_token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$corpid}&corpsecret={$secret}";
            $res = HTTPHandler::http_get_data($get_token_url);
            if($res===false) {
                abort(500,"Cannot connected to Weixin Server.");
            }
            $json_obj = json_decode($res,true);
            //根据access_token查询用户信息
            $token = $json_obj['access_token'];
            if($token) {
                Cache::put('qyh_access_token', $token, floor(($json_obj['expires_in']-60)/60));
            }
        }
        return $token;
    }
}